package com.jfireframework.sql.transfer.resultset.impl;

import java.sql.ResultSet;
import java.util.LinkedList;
import java.util.List;
import com.jfireframework.baseutil.StringUtil;
import com.jfireframework.sql.transfer.resultset.ResultSetTransfer;

public abstract class AbstractResultsetTransfer<T> implements ResultSetTransfer<T>
{
	
	@Override
	public T transfer(ResultSet resultSet) throws Exception
	{
		if (resultSet.next())
		{
			T result = valueOf(resultSet);
			if (resultSet.next())
			{
				throw new IllegalArgumentException(StringUtil.format("存在2行数据，不符合返回值要求。"));
			}
			else
			{
				return result;
			}
		}
		else
		{
			return null;
		}
	}
	
	@Override
	public List<T> transferList(ResultSet resultSet) throws Exception
	{
		List<T> list = new LinkedList<T>();
		while (resultSet.next())
		{
			list.add(valueOf(resultSet));
		}
		return list;
	}
	
	protected abstract T valueOf(ResultSet resultSet) throws Exception;
}
